/**
 * 设置cookie
 * @param name cookie的名称
 * @param value cookie的值
 * @param day cookie的过期天数
 */
function setCookie(name, value, day) {
  if (day !== 0) {
    //当设置的时间等于0时，不设置expires属性，cookie在浏览器关闭后删除
    var expires = day * 24 * 60 * 60 * 1000;
    var date = new Date(+new Date() + expires);
    document.cookie = name + "=" + escape(value) + ";expires=" + date.toUTCString();
  } else {
    document.cookie = name + "=" + escape(value);
  }
}

// 获取cookie
function getCookie(name) {
  var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
  if (arr = document.cookie.match(reg))
    return unescape(arr[2]);
  else
    return '';
}

/*删除Cookie*/
function delCookie(name) {
  var _exdate = new Date();
  _exdate.setTime(_exdate.getTime() - 1);
  var _cookiesVal = getCookie(name);
  if (_cookiesVal != null) {
    document.cookie = name + "=" + _cookiesVal + ";expires=" + _exdate.toGMTString();
  }
}

// 重载cookie
function resetCookie() {
  // token
  if (localStorage.getItem('token') && (getCookie('token') == 'null' || getCookie('token') == '')) {
    setCookie('token', localStorage.getItem('token'), 1)
  }
  if (!localStorage.getItem('token')) {
    delCookie('token')
  }
  // userinfo
  if (localStorage.getItem('userInfo') && (getCookie('userInfo') == 'null' || getCookie('userInfo') == '')) {
    setCookie('userInfo', localStorage.getItem('userInfo'), 1)
  }
  if (!localStorage.getItem('userInfo')) {
    delCookie('userInfo')
  }
}
resetCookie()

// 判断微信内置浏览器
function isWeiXin() {
  var ua = window.navigator.userAgent.toLowerCase();
  if (ua.match(/MicroMessenger/i) == "micromessenger") {
    return true;
  } else {
    return false;
  }
}

// 判断哪个端
function isMobile() {
  if ((navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)))
    return true;
  else
    return false;
}

// 追加js
window._agl && window._agl.push(['track', ['success', {t: 3}]])